package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserSearchServiceImpl  extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {


    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override/*查询 apUserSearch*/
    public ResponseResult findUserSearch(UserSearchDto dto) {

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不存在");
        }

        /*获取用户id*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }

        /*查询实体*/
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findApBehaviorEntry(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"实体不存在");
        }

        /*mybatis 的分页*/
        Page pageParam = new Page(dto.getPageNum(), dto.getPageSize());

        IPage result = page(pageParam, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, apBehaviorEntry.getId())
                .eq(ApUserSearch::getStatus, 1));

        return ResponseResult.okResult(result.getRecords());
    }


    @Override/*逻辑删除 apUserSearch*/
    public ResponseResult delUserSearch(UserSearchDto dto) {

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不存在");
        }

        /*获取用户id*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }

        /*查询实体*/
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findApBehaviorEntry(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"实体不存在");
        }

        this.update(Wrappers.<ApUserSearch>lambdaUpdate().eq(ApUserSearch::getEntryId,apBehaviorEntry.getId())
        .eq(ApUserSearch::getId,dto.getId()).set(ApUserSearch::getStatus,0));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @Async("taskExecutor")//异步加载到线程池执行
    public void insert(Integer entryId, String searchWords) {

        if (entryId==null || StringUtils.isBlank(searchWords)){
            return;
        }

        /*判重*/
        ApUserSearch apUserSearch = this.getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId)
                .eq(ApUserSearch::getKeyword, searchWords));

        if (apUserSearch!=null){

            if (apUserSearch.getStatus()==1){
                return;
            }

            if (apUserSearch.getStatus()==0){
                apUserSearch.setStatus(1);
                this.updateById(apUserSearch);
                return;
            }
        }else {
            //3.如果不存在，保存新的数据
            apUserSearch = new ApUserSearch();
            apUserSearch.setEntryId(entryId);
            apUserSearch.setStatus(1);
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setCreatedTime(new Date());
            this.save(apUserSearch);
        }

    }
}
